public class SimpleNotepad implements INotepad {

	//Fields
	private Page[] pages;
	//Constructors
	public SimpleNotepad(int numberOfPages){
		if (numberOfPages >= 0) {
			this.pages = new Page[numberOfPages];
			for (int i = 0; i < numberOfPages; i++) {
				this.pages[i] = new Page();
			}
		}
	}
	//Methods
	public Page getPage(int pageNumber){
		int currentPageNumber = pageNumber-1;
		if (checkPageNumber(currentPageNumber)) {
			return this.pages[currentPageNumber];
		}
		return null;
	}
	
	@Override
	public void addTextToPage(int pageNumber, String text) {
		int currentPageNumber = pageNumber - 1;
		if (checkPageNumber(currentPageNumber)
				&& checkIfArrayAndCurrentPageIsNull(currentPageNumber)
				&& text != null) {
			String pageText = this.pages[currentPageNumber].getText();
			pageText += text;
			this.pages[currentPageNumber].setText(pageText);
			System.out.println("Text is added");
		}
	}

	@Override
	public void overWriteTextOnPage(int pageNumber, String text) {
		int currentPageNumber = pageNumber-1;
		if (checkPageNumber(currentPageNumber)&&checkIfArrayAndCurrentPageIsNull(currentPageNumber)) {
			if(text != null){
				this.pages[currentPageNumber].setText(text);
				System.out.println("Text is OverWritten");
			}
		}
	}

	@Override
	public void deleteTextOnPage(int pageNumber) {
		int currentPageNumber = pageNumber-1;
		if (checkPageNumber(currentPageNumber) && checkIfArrayAndCurrentPageIsNull(currentPageNumber)) {
				this.pages[currentPageNumber].deleteText();
				System.out.println("Text was deleted");
		}
	}

	@Override
	public void showNotepad() {
		for(Page x : pages){
			System.out.println(x.viewPage());
			System.out.println("\n\n");
		}
		
	}
	
	
	boolean checkPageNumber(int currentPageNumber){
		if (currentPageNumber >= 0 && currentPageNumber < this.pages.length){
			return true;
		}else{
			System.out.println("No page!");
		}
		return false;
	}
	
	boolean checkIfArrayAndCurrentPageIsNull(int currentPageNumber){
		if (this.pages != null && this.pages[currentPageNumber] != null){
			return true;
		}else{
			System.out.println("No Notepad or no Page");
		}
		return false;
	}
	@Override
	public boolean searchWord(String word) {
		for(Page x : pages){
			if(x.searchWord(word)){
				return true;
			}
		}
		return false;
	}
	@Override
	public void printAllPagesWithDigits() {
		for(Page x : pages){
			if(x.containsDigits()){
				System.out.println(x.viewPage());
			}
		}
		
	}

}
